package com.dq.controller.op.opmerchant;

import com.dq.cache.J2CacheUtils;
import com.dq.exception.customException.AdminException;
import com.dq.exception.customException.ApiException;
import com.dq.controller.in.CategoryQuery;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.category.Category;
import com.dq.service.AllService;
import com.dq.utils.TreeBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * @author heshuaijun
 * @date 2019/9/1711:39
 */
@Api(description = "后台分类管理")
@RestController
@RequestMapping("/sys/category")
public class CategoryController extends AllService {
    @ApiOperation("所有分类列表")
    @GetMapping("/list")
    public ListVo<Category.Vo> list() {
        //查询列表数据
        List<Category> categoryList = categoryRepository.findByParentCategoryAndDeletedFalse((Category) J2CacheUtils.get("NullCategory"));
        return new ListVo((List<Category.Vo>) VO.toVOCollection(categoryList), categoryList.size());
    }

    @ApiOperation("所有分类列表")
    @GetMapping("/all")
    public Vo<Category.Vo> all() {
        if (shiroService.isSuperAdmin()) {
            List<Category> categoryList = categoryRepository.findByParentCategoryAndDeletedFalse((Category) J2CacheUtils.get("NullCategory"));
            return new Vo(VO.toVOCollection(categoryList));
        }
        List<Category> categoryList = categoryRepository.findByParentCategoryAndOrgAndDeletedFalse((Category) J2CacheUtils.get
                ("NullCategory"), shiroService.getDept());
        return new Vo(VO.toVOCollection(categoryList, false));
    }

    @ApiOperation("所有分类树形列表")
    @PostMapping("/listTreeCategory")
    public Vo listTreePermission(@RequestBody CategoryQuery categoryQuery) throws AdminException {
       return categoryService.getCategoryTree(categoryQuery);
    }


    @ApiOperation("分类列表")
    @GetMapping("/catagoryList")
    public Vo<Category.Vo> catagoryList() throws ApiException {
        List<Category> categories = categoryRepository.findByDeletedFalseAndParentCategoryIsNotNull();
        List<Category.Vo> vos = TreeBuilder.categoryBuildTree(new HashSet<>((List<Category.Vo>) VO.toVOCollection(categories)));
        return  new Vo(vos);
    }

    @ApiOperation("检索下级分类")
    @GetMapping("/find/{parentId}")
    @RequiresPermissions("sys:category:findByParentId")
    public ListVo<Category.Vo> findByParentId(@PathVariable @ApiParam(value = "分类名称") Long parentId) throws AdminException {
        List<Category> categories = categoryService.getParCategory(parentId);
        return new ListVo((List<Category.Vo>) VO.toVOCollection(categories), categories.size());
    }

    @ApiOperation("新增分类")
    @PostMapping("/add/{parentId}")
    @RequiresPermissions("sys:category:add")
    public VO add(@RequestBody Category category, @PathVariable @ApiParam("上级id") Long parentId) throws AdminException {
        Category parent = dao.get(Category.class, parentId);
        if (null == parent) {
            throw new AdminException("上级菜单不存在");
        }
        category.setParentCategory(parent);

        Category save = categoryService.save(category);
        return new Vo(save.toVO());
    }

    @ApiOperation("编辑分类")
    @PostMapping("/update/{categoryId}")
    @RequiresPermissions("sys:category:update")
    public VO update(@RequestBody Category updateCategory, @PathVariable @ApiParam(value = "分类id") Long categoryId) throws AdminException {
        updateCategory.setId(categoryId);

        Category save = categoryService.update(updateCategory);
        return new Vo(save.toVO());
    }


    @ApiOperation("删除分类")
    @PostMapping("/delete/{categoryId}")
    @RequiresPermissions("sys:category:delete")
    public VO delete(@PathVariable @ApiParam(value = "分类id") Long categoryId) throws AdminException {
        Category category = dao.get(Category.class, categoryId);
        if (category == null) {
            throw new AdminException("未找到此分类");
        }
        List<Category> sons = categoryRepository.findByParentCategoryAndDeletedFalse(category);
        if (!sons.isEmpty()) {
            throw new AdminException("请先删除子级对象");
        }

        category.setDeleted(true);
        categoryRepository.save(category);
        return new Vo(HttpStatus.OK);
    }
}
